package com.yylending.las.personal.bandcard.cardinfo;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yylending.las.R;
import com.yylending.las.Constants;
import com.yylending.las.personal.bandcard.BoundBankCards;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ying_he on 2017/6/8.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class BankCardAdapter extends RecyclerView.Adapter<BankCardAdapter.ViewHolder> {
    private ArrayList<BoundBankCards.BankInfo> mBankInfoList;

    public BankCardAdapter(ArrayList<BoundBankCards.BankInfo> bankInfoList) {
        super();
        mBankInfoList = bankInfoList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_item_bank_card, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.bindView(mBankInfoList.get(position));
    }

    @Override
    public int getItemCount() {
        if (mBankInfoList != null){
            return mBankInfoList.size();
        }
        return 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        View itemView;
        @BindView(R.id.bank_bg_tv)
        ImageView mBankBgTv;
        @BindView(R.id.bank_card_tv)
        TextView mBankCardTv;

        ViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            ButterKnife.bind(this, itemView);
        }

        void bindView(BoundBankCards.BankInfo bankInfo) {
            int imgResId = -1;
            switch (bankInfo.getBank()) {
                case Constants.BANK_CODE_ICBC:
                    imgResId = R.drawable.bg_bank_icbc;
                    break;
                case Constants.BANK_CODE_ABC:
                    imgResId = R.drawable.bg_bank_abc;
                    break;
                case Constants.BANK_CODE_BOC:
                    imgResId = R.drawable.bg_bank_boc;
                    break;
                case Constants.BANK_CODE_CCB:
                    imgResId = R.drawable.bg_bank_ccb;
                    break;
                case Constants.BANK_CODE_CITIC:
                    imgResId = R.drawable.bg_bank_citic;
                    break;
                case Constants.BANK_CODE_CEB:
                    imgResId = R.drawable.bg_bank_ceb;
                    break;
                case Constants.BANK_CODE_HXB:
                    imgResId = R.drawable.bg_bank_hxb;
                    break;
                case Constants.BANK_CODE_GDB:
                    imgResId = R.drawable.bg_bank_gdb;
                    break;
                case Constants.BANK_CODE_PINGAN:
                    imgResId = R.drawable.bg_bank_pingan;
                    break;
                case Constants.BANK_CODE_CIB:
                    imgResId = R.drawable.bg_bank_cib;
                    break;
                case Constants.BANK_CODE_CMB:
                    imgResId = R.drawable.bg_bank_cmb;
                    break;
                case Constants.BANK_CODE_SPDB:
                    imgResId = R.drawable.bg_bank_spdb;
                    break;
                case Constants.BANK_CODE_PSBC:
                    imgResId = R.drawable.bg_bank_psbc;
                    break;
                case Constants.BANK_CODE_CMBC:
                    imgResId = R.drawable.bg_bank_cmbc;
                    break;
                case Constants.BANK_CODE_BEIJING:
                    imgResId = R.drawable.bg_bank_beijing;
                    break;
                default:
                    break;
            }
            if (imgResId != -1) {
                mBankBgTv.setImageResource(imgResId);
            }
            if (!TextUtils.isEmpty(bankInfo.getNumber())) {
                int length = bankInfo.getNumber().length();
                if (length > 4) {
                    mBankCardTv.setText(bankInfo.getNumber().substring(length - 4, length));
                }else {
                    mBankCardTv.setText(bankInfo.getNumber());
                }
            }
        }
    }
}
